package com.service.impl;

import com.Exception.NormalException.ChatException.MessageTypeException;
import com.Exception.NormalException.FileExceptions.FileFormatException;
import com.WebSocket.SocketData;
import com.config.FileConfigure;
import com.entity.Message;
import com.entity.User;
import com.WebSocket.WebSocket;
import com.mapper.GroupUserMapper;
import com.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.Session;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ChatServiceImpl implements ChatService {
    @Autowired
    private GroupUserMapper groupUserMapper;

    @Autowired
    private FileConfigure fileConfigure;

    @Autowired
    private FileService fileService;

    private Set<WebSocket> webSockets = WebSocket.webSockets;
    // 用来存在线连接用户信息
    private ConcurrentHashMap<String, Session> sessionPool = WebSocket.sessionPool;

/*    public boolean sendMessage(Message message) throws TargetTypeException {
        String targetType = message.getTargetType();
        if(targetType.equals("user")) {
            return this.sendMessageToUser(message);
        }
        if(targetType.equals("group")) {
            return this.sendMessageToGroup(message);
        }
        throw new TargetTypeException();
    }*/

    @Override
    public boolean sendMessageToUser(Message message) {
        // 获取接收用户id
        String targetId = String.valueOf(message.getReceiveUserId());

        // 设置SocketData
        SocketData socketData = new SocketData();
        socketData.setMessage(message);
        socketData.setType("message");

        // 发送信息到接收用户
        WebSocket.sendOneMessage(targetId, socketData);
        return true;
    }

    @Override
    public boolean sendMessageToGroup(Message message, List<User> userList) {
        // 为群聊里所有群成员发送信息
         for(User user : userList) {
             if(user.getId().equals(message.getSendUserId())) {
                 continue;
             }
             String targetId = String.valueOf(user.getId());
             SocketData socketData = new SocketData();
             socketData.setMessage(message);
             socketData.setType("message");
             WebSocket.sendOneMessage(targetId, socketData);
         }
         return true;
    }

    @Override
    public void checkMessageType(Message message) throws MessageTypeException, FileFormatException, IOException {
        switch (message.getMessageType()) {
            case("text"):
                break;
            case("voice"):
                this.saveVoice(message);
                break;
            case("video"):
                this.saveVideo(message);
                break;
            case("file"):
                this.saveFile(message);
                break;
            case("picture"):
                this.savePicture(message);
                break;
            default:
                throw new MessageTypeException();
        }
    }

    protected void saveFile(Message message) throws FileFormatException, IOException {
        MultipartFile multipartFile = message.getMultipartFile();
        String name = message.getSendUserId() + "_" + message.getReceiveUserId() + "_" + multipartFile.getOriginalFilename();
        String url = this.fileService.saveFile(multipartFile, "file/", name, true);
        message.setMultipartFile(null);
        message.setFileUrl(url);
    }

    protected void savePicture(Message message) throws FileFormatException, IOException {
        MultipartFile multipartFile = message.getMultipartFile();
        String name = message.getSendUserId() + "_" + message.getReceiveUserId() + "_" + multipartFile.getOriginalFilename();
        String url = this.fileService.saveFile(multipartFile, "picture/", name, true);
        message.setMultipartFile(null);
        message.setFileUrl(url);
    }

    protected void saveVoice(Message message) throws FileFormatException, IOException {
        MultipartFile multipartFile = message.getMultipartFile();
        String name = message.getSendUserId() + "_" + message.getReceiveUserId() + "_" + multipartFile.getOriginalFilename();
        String url = this.fileService.saveFile(multipartFile, "voice/", name, true);
        message.setMultipartFile(null);
        message.setFileUrl(url);
    }

    protected void saveVideo(Message message) throws FileFormatException, IOException {
        MultipartFile multipartFile = message.getMultipartFile();
        String name = message.getSendUserId() + "_" + message.getReceiveUserId() + "_" + multipartFile.getOriginalFilename();
        String url = this.fileService.saveFile(multipartFile, "video/", name, true);
        message.setMultipartFile(null);
        message.setFileUrl(url);
    }
}
